package view;

import controller.UserController;
import java.util.Scanner;
import model.User;

public class UserView {
    private UserController userController;
    private Scanner scanner;

    public UserView(UserController userController) {
        this.scanner = new Scanner(System.in);
        this.userController = userController;
    }

    public void showMenu() {
        while(true) {
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.println("3. 更新用户");
            System.out.println("4. 退出");
            int choice = this.scanner.nextInt();
            this.scanner.nextLine();
            switch (choice) {
                case 1:
                    this.login();
                    this.showMenu();
                    break;
                case 2:
                    this.register();
                    break;
                case 3:
                    this.updateUser();
                    break;
                case 4:
                    return;
            }
        }
    }

    private void login() {
        System.out.print("输入用户名: ");
        String username = this.scanner.nextLine();
        System.out.print("输入密码: ");
        String password = this.scanner.nextLine();
        User user = this.userController.login(username, password);
        if (user != null) {
            System.out.println("登陆成功.");
        } else {
            System.out.println("登陆失败.");
        }

    }

    private void register() {
        System.out.print("输入用户名: ");
        String username = this.scanner.nextLine();
        System.out.print("输入密码: ");
        String password = this.scanner.nextLine();
        System.out.print("是否为管理(true/false): ");
        boolean isAdmin = this.scanner.nextBoolean();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAdmin(isAdmin);
        this.userController.registerUser(user);
        System.out.println("用户注册.");
    }

    private void updateUser() {
        System.out.print("输入用户名: ");
        String username = this.scanner.nextLine();
        System.out.print("输入新密码: ");
        String password = this.scanner.nextLine();
        System.out.print("是否为管理 (true/false): ");
        boolean isAdmin = this.scanner.nextBoolean();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAdmin(isAdmin);
        this.userController.updateUser(user);
        System.out.println("用户更新.");
    }
}
